/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.LogisticsControl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.Workspace.JobManagerBean;
import merlion.common.util.Consts;
import merlion.scm.lcm.entity.DeliveryOrder;
import merlion.scm.lcm.entity.PackingOrder;
import merlion.scm.lcm.entity.PickingOrder;
import merlion.scm.lcm.entity.ShippingOrder;
import merlion.scm.lcm.session.LogisticControlSessionBeanLocal;

/**
 *
 * @author Kenn
 */
@ManagedBean(name = "lcManagerBean")
@ViewScoped
public class LogisticsControlManagerBean {

    @EJB
    private LogisticControlSessionBeanLocal lcSessionBean;
    private Integer page;
    private List<DeliveryOrder> dList;
    private PickingOrder pickingOrder;
    private PackingOrder packingOrder;
    private ShippingOrder shippingOrder;
    private DeliveryOrder[] selected;
    @ManagedProperty(value = "#{jobManagerBean}")
    private JobManagerBean jobManager;

    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------
    public LogisticsControlManagerBean() {
    }

    @PostConstruct
    public void init() {
        page = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("page");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("page");
        
        dList = (List<DeliveryOrder>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dList");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("dList");
        
        pickingOrder = (PickingOrder) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("pickingOrder");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("pickingOrder");
        
        packingOrder = (PackingOrder) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("packingOrder");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("packingOrder");
        
        shippingOrder = (ShippingOrder) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("shippingOrder");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("shippingOrder");
        
        if (page == null) {
            page = 10;
        }
        if (dList == null) {
            dList = getConfirmedOrderList();
        }
    }

    //--------------------------------------------------------------------------
    //                          Manager Bean Methods
    //--------------------------------------------------------------------------
    public List<DeliveryOrder> getConfirmedOrderList() {
        return lcSessionBean.getConfirmedOrderList();
    }
    
    public List<DeliveryOrder> getProcessingOrderList() {
        return lcSessionBean.getProcessingOrderList();
    }
    
    public List<DeliveryOrder> getProcessedOrderList() {
        return lcSessionBean.getProcessedOrderList();
    }
    
    public String viewOrderList(){
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("page", page);
        
        return "viewOrderList?faces-redirect=true";
    }

    public String setOrderPerPage() {
        dList = lcSessionBean.getConfirmedOrderList();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dList", dList);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("page", page);
        
        return "viewOrderList?faces-redirect=true";
    }
    
    public void processOrder(ActionEvent event){
        
        for(int i = 0; i < selected.length; i++){
            DeliveryOrder d = selected[i];
            lcSessionBean.generateLogisticControlOrders(d);
           // lcSessionBean.rankDeliveryOrder(d);
        }
        
    }
    
    public boolean disablePacking(Long id){
        DeliveryOrder d = lcSessionBean.getDeliveryOrderById(id);
        if(d.getStatus().equals("Picking"))
            return true;
        return false;
    }
    
    public boolean disableShipping(Long id){
        DeliveryOrder d = lcSessionBean.getDeliveryOrderById(id);
        if(d.getStatus().equals("Packing") || d.getStatus().equals("Picking"))
            return true;
        return false;
    }
    
    public boolean disableShipped(Long id){
        DeliveryOrder d = lcSessionBean.getDeliveryOrderById(id);
        if(!d.getStatus().equals("Shipped"))
            return true;
        return false;
    }
    
    public String viewPickingOrder(Long id){
        DeliveryOrder d = lcSessionBean.getDeliveryOrderById(id);
        pickingOrder = d.getPickingOrder();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("pickingOrder", pickingOrder);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("page", page);
        
        
        return "viewOrderListPickingOrder?faces-redirect=true";
    }
    
    public String viewPackingOrder(Long id){
        DeliveryOrder d = lcSessionBean.getDeliveryOrderById(id);
        packingOrder = d.getPackingOrder();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("packingOrder", packingOrder);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("page", page);
        
        
        return "viewOrderListPackingOrder?faces-redirect=true";
    }
    
    public String viewShippingOrder(Long id){
        DeliveryOrder d = lcSessionBean.getDeliveryOrderById(id);
        shippingOrder = d.getShippingOrder();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("shippingOrder", shippingOrder);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("page", page);
        
        
        return "viewOrderListShippingOrder?faces-redirect=true";
    }
     
    public String setPickingOrderDone(){
        pickingOrder = lcSessionBean.setPickingOrderDone(pickingOrder);
        dList = getConfirmedOrderList();
        return null;
    }
    
    public String setPackingOrderDone(){
        packingOrder = lcSessionBean.setPackingOrderDone(packingOrder);
        dList = getConfirmedOrderList();
        return null;
    }
    
    public String setShippingOrderDone(){
        shippingOrder = lcSessionBean.setShippingOrderDone(shippingOrder);
        dList = getConfirmedOrderList();
        
        if (shippingOrder.getDeliveryOrder().getSalesOrder().getPaymentMode().booleanValue() == Boolean.FALSE)
           jobManager.assignJobInvoiceCC(shippingOrder.getDeliveryOrder().getId());
        return null;
    }

    //--------------------------------------------------------------------------
    //                            Getter and Setter
    //--------------------------------------------------------------------------
    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public List<DeliveryOrder> getdList() {
        return dList;
    }

    public void setdList(List<DeliveryOrder> dList) {
        this.dList = dList;
    }

    public PackingOrder getPackingOrder() {
        return packingOrder;
    }
    
    public void setPackingOrder(PackingOrder packingOrder) {
        this.packingOrder = packingOrder;
    }

    public PickingOrder getPickingOrder() {
        return pickingOrder;
    }

    public void setPickingOrder(PickingOrder pickingOrder) {
        this.pickingOrder = pickingOrder;
    }

    public ShippingOrder getShippingOrder() {
        return shippingOrder;
    }

    public void setShippingOrder(ShippingOrder shippingOrder) {
        this.shippingOrder = shippingOrder;
    }

    public DeliveryOrder[] getSelected() {
        return selected;
    }

    public void setSelected(DeliveryOrder[] selected) {
        this.selected = selected;
    }

    public JobManagerBean getJobManager() {
        return jobManager;
    }

    public void setJobManager(JobManagerBean jobManager) {
        this.jobManager = jobManager;
    }

}
